﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Z1029.NoteEditor
{
    public class NoteChooser : MonoBehaviour
    {
        public Camera cam;
        public GameObject g;
        public RectTransform re;
        public bool multiChoose;

        public List<Note> selectNotes;
        public Note selectNote;
        public EventBase selectEvent;
        public List<EventBase> selectEvents;
        // Update is called once per frame
        void Update()
        {
            if (ViewCtrl.viewType == ViewCtrl.ViewType.Hand)
            {
                multiChoose = LevelController.instance.multiSelectNote;
                if (CanGenerateNote())
                {
                    if (LevelController.instance.editMode == LevelController.EditMode.Event && Input.GetMouseButton(1))
                    {
                        Ray r = new Ray(g.transform.position, Vector3.forward);
                        RaycastHit[] rhs = Physics.RaycastAll(r);
                        if (rhs.Length > 0)
                        {
                            foreach(RaycastHit rh in rhs)
                            {
                                Destroy(rh.transform.gameObject);
                            }
                        }
                    }
                    if ((!multiChoose && Input.GetMouseButton(0)) || (multiChoose && Input.GetMouseButton(0)))
                    {

                        Ray r = new Ray(g.transform.position, Vector3.forward);
                        RaycastHit[] rhs = Physics.RaycastAll(r);
                        if (rhs.Length > 0)
                        {
                            if (multiChoose)
                            {
                                for (int i = 0; i < rhs.Length; i++)
                                {
                                    if (LevelController.instance.editMode == LevelController.EditMode.Note)
                                    {
                                        Note n = rhs[i].transform.gameObject.GetComponent<Note>();
                                        n.thisRenderer.color = Color.red;
                                        if (!selectNotes.Contains(n))
                                            selectNotes.Add(n);
                                    }
                                    else
                                    {
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                if (LevelController.instance.editMode == LevelController.EditMode.Event)
                                {
                                    if (selectEvent  != null)
                                    {
                                        selectEvent.thisRenderer.color = (Color.blue + Color.white )/2;
                                        selectEvent.isEditing = false;
                                        selectEvent = null;
                                    }
                                    if (rhs.Length == 1)
                                    {
                                        selectEvent = rhs[0].transform.gameObject.GetComponent<EventBase>();
                                    }
                                    else if (rhs.Length > 1)
                                    {
                                        //List<Note> ln = new List<Note>();
                                        selectEvent = rhs[0].transform.gameObject.GetComponent<EventBase>();
                                        for (int i = 0; i < rhs.Length; i++)
                                        {
                                            EventBase n = rhs[i].transform.gameObject.GetComponent<EventBase>();
                                            if (Vector3.Distance(n.transform.position, g.transform.position) < Vector3.Distance(selectNote.transform.position, g.transform.position))
                                            {
                                                selectEvent = n;
                                            }
                                            //n.thisRenderer.color = Color.red;
                                            //ln.Add(n);
                                        }
                                    }
                                    selectEvent.thisRenderer.color = Color.red;
                                    selectEvent.isEditing = true;
                                }
                                else
                                {
                                    if (selectNote != null)
                                    {
                                        selectNote.thisRenderer.color = Color.white;
                                        selectNote = null;
                                    }
                                    if (rhs.Length == 1)
                                    {
                                        selectNote = rhs[0].transform.gameObject.GetComponent<Note>();
                                    }
                                    else if (rhs.Length > 1)
                                    {
                                        selectNote = rhs[0].transform.gameObject.GetComponent<Note>();
                                        for (int i = 0; i < rhs.Length; i++)
                                        {
                                            Note n = rhs[i].transform.gameObject.GetComponent<Note>();
                                            if (Vector3.Distance(n.transform.position, g.transform.position) < Vector3.Distance(selectNote.transform.position, g.transform.position))
                                            {
                                                selectNote = n;
                                            }
                                        }
                                    }
                                    selectNote.thisRenderer.color = Color.red;
                                }
                            }
                        }
                        else
                        {
                            if (NoteDataEditManager.instance.isEditing) NoteDataEditManager.instance.CloseWindow();
                            if (EventDataEditManager.instance.isEditing) EventDataEditManager.instance.CloseWindow();
                        }
                        //print("Mouse:"+Input.mousePosition +"Rect:"+ RectToMouse(re));
                    }
                    if (Input.GetMouseButtonUp(0))
                    {
                        if (selectNote != null)
                        {
                            //selectNote.thisRenderer.color = Color.white;
                            NoteDataEditManager.instance.ShowNoteEditWindow(selectNote);
                            selectNote = null;
                        }
                        if (selectNotes.Count > 0)
                        {
                            selectNotes.Sort();
                            NoteDataEditManager.instance.ShowNoteEditWindow(selectNotes);
                            selectNotes = new List<Note>();
                        }

                        if (selectEvent != null)
                        {
                            //selectNote.thisRenderer.color = Color.white;
                            EventDataEditManager.instance.ShowEventEditWindow(selectEvent);
                            selectEvent = null;
                        }
                        if (selectEvents.Count > 0)
                        {
                            selectEvents.Sort();
                            //NoteDataEditManager.instance.ShowNoteEditWindow(selectNotes);
                            selectEvents = new List<EventBase>();
                        }
                    }
                }
            }
        }

        public Vector2 RectToMouse(RectTransform rect)
        {
            Vector2 res = Vector2.zero;
            res.x = rect.anchoredPosition.x + Screen.width / 2 - rect.sizeDelta.x / 2;
            res.y = rect.anchoredPosition.y + Screen.height / 2 - rect.sizeDelta.y / 2;
            return res;
        }

        public bool CanGenerateNote()
        {
            return Input.mousePosition.x - RectToMouse(re).x < re.sizeDelta.x && Input.mousePosition.x - RectToMouse(re).x > 0;
        }
    }
}